package algorithm.binarysearch;

public class BinarySearch {
    public static void main(String[] args) {
        int[] arr = {2,3,4,22,44,5,77,1,100};
        int index = binarySearch(arr,100);
        System.out.println("index="+index);

    }
    //二分查找
    public static int binarySearch(int[] arr,int target){
        int left = 0;
        int right = arr.length-1;
        while(left<=right){
            int mid = (left+right)/2;
            if(arr[mid]==target){
                return mid;
            }else if(arr[mid]>target){
                right = mid-1;
            }else {
                left = mid+1;
            }
        }
        return -1;
    }
}
